//
//  QKEasyTestView.m
//  QKEasyTest
//
//  Created by ChiCo on 2018/6/4.
//

#import "QKEasyTestView.h"

@interface QKEasyTestView ()
@property (nonatomic,strong)NSArray <NSString *>*titleArray;
@property (nonatomic,strong)UIScrollView *bodyView;
@end

@implementation QKEasyTestView

- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray<NSString *> *)titleArray {
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArray = titleArray;
    }
    return self;
}

- (void)setTitleArray:(NSArray<NSString *> *)titleArray {
    _titleArray = titleArray;
    CGRect rect = self.frame;
    rect.origin = CGPointZero;
    CGFloat longWidth = (rect.size.width-30)/3*2;
    CGFloat longR = longWidth + 20;
    CGFloat shortWidth = (rect.size.width-30)/3;
    CGFloat shortR = shortWidth + 20;
    CGFloat height = 100;
    [self.bodyView setFrame:rect];
    BOOL isLong = YES;
    CGFloat originY = 10;
    for (NSUInteger index = 0; index < [_titleArray count]; index ++) {
        NSString *title = [_titleArray objectAtIndex:index];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:title forState:UIControlStateNormal];
        [self.bodyView addSubview:button];
        [self setButtonStyle:button index:index];
       
        if (isLong) {
            [button setFrame:CGRectMake((index%2 == 0)? 10:shortR,originY, longWidth, height)];
        } else {
            [button setFrame:CGRectMake((index%2 == 0)? 10:longR,originY, shortWidth, height)];
        }
        button.layer.cornerRadius = 6;
        button.clipsToBounds = YES;
        [button.titleLabel setNumberOfLines:0];
        button.tag = 1000 + index;
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        if (index%2 == 0) {
            isLong = !isLong;
        } else {
            originY = originY + height + 10;

        }
       
    }
    
    if ([_titleArray count] % 2 == 0) {
        [self.bodyView setContentSize:CGSizeMake(0, originY)];
    } else {
        [self.bodyView setContentSize:CGSizeMake(0, originY + height + 10)];
    }
    
}

- (UIScrollView *)bodyView {
    if (!_bodyView) {
        _bodyView = [[UIScrollView alloc] init];
        [self addSubview:_bodyView];
    }
    return _bodyView;
}

- (void)setButtonStyle:(UIButton *)button index:(NSUInteger)index {
    NSArray *colorArray = @[@[@"#ff6600",@"#FFFFFF"],
                            @[@"#00FF00",@"#5C3317"],
                             @[@"#9F5F9F",@"#FFFFFF"],
                             @[@"#32CD99",@"#FFFFFF"],
                             @[@"#238123",@"#FFFFFF"]];
    NSUInteger row = index % [colorArray count];
    NSArray *arr = [colorArray objectAtIndex:row];
    NSString *backColor = [arr firstObject];
    NSString *titleColor = [arr lastObject];
    [button setBackgroundColor:[self colorWithHexString:backColor]];
    [button setTitleColor:[self colorWithHexString:titleColor] forState:UIControlStateNormal];
}

- (void)clickButton:(UIButton *)button
{
    NSUInteger index = (NSUInteger)(button.tag - 1000);
    if (self.didSelectedItem) {
        self.didSelectedItem(self.titleArray[index]);
    }
    
}
- (UIColor *)colorWithHexString:(NSString *)hexString
{
    if ([hexString hasPrefix:@"0x"] || [hexString hasPrefix:@"0X"]) {
        hexString = [hexString substringFromIndex:2];
    } else if ([hexString hasPrefix:@"#"]) {
        hexString = [hexString substringFromIndex:1];
    }
    
    unsigned int value = 0;
    BOOL flag = [[NSScanner scannerWithString:hexString] scanHexInt:&value];
    if(NO == flag)
        return [UIColor clearColor];
    CGFloat r, g, b, a;
    a = 1.0;
    b = value & 0x0000FF;
    value = value >> 8;
    g = value & 0x0000FF;
    value = value >> 8;
    r = value;
    
    return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0];
}

@end
